//
//  RegisterViewController.m
//  觅影
//
//  Created by lanou on 16/4/12.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginViewController.h"

@interface RegisterViewController () <UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *keyTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet UITextField *sureTextField;


@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    self.keyTextField.delegate = self;
    self.passwordTextField.delegate = self;
    self.sureTextField.delegate  = self;
    
}

- (void)registerAction:(UIButton *)button
{
    
    NSString *username = self.keyTextField.text;
    NSString *password = self.passwordTextField.text;
    NSString *sure = self.sureTextField.text;
    if (![password isEqualToString:sure]) {

        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入不一致" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [sheet addAction:cancelAction];
            [self presentViewController:sheet animated:YES completion:nil];
        });
    
    }
    else
    {
        NSDictionary *parDic = @{@"gender":@"2",@"password":password,@"username":username};
        [RequestManager requestWithUrl:@"http://static.v6v7.com/snowman_api/user/username_register" requestType:RequestTypePOST parDic:parDic finshed:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSString *msgString = dic[@"msg"];
            NSString *msg = [msgString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
        if ([msg isEqualToString:@"请求成功"])  {
            
            dispatch_async(dispatch_get_main_queue(), ^{

                // 注册成功，跳转到注册界面
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                loginVC.registerVC = self;
                [UIView transitionFromView:self.view toView:loginVC.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];                [self presentViewController:loginVC animated:NO completion:nil];
            });
            
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"昵称被占用" preferredStyle:UIAlertControllerStyleActionSheet];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                [sheet addAction:cancelAction];
                [self presentViewController:sheet animated:YES completion:nil];
                });
        }
        } error:^(NSError *error) {
            NSLog(@"请求错误");
        }];

    }
  
}

#pragma mark --- 点击空白回收键盘 ---
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.keyTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    [self.sureTextField resignFirstResponder];
}

#pragma maek --- 点击return回收键盘 ---
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.keyTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    [self.sureTextField resignFirstResponder];
    return YES;

}


// 测试按钮
- (IBAction)textButton:(id)sender
{
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.registerVC = self;
    [UIView transitionFromView:self.view toView:loginVC.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    [self presentViewController:loginVC animated:NO completion:nil];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
